package com.kxmall.admin.controller.vehicle;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.kxmall.admin.controller.vehicle.service.IKxVehicleGoodsService;
import com.kxmall.common.annotation.Log;
import com.kxmall.common.annotation.RepeatSubmit;
import com.kxmall.common.core.controller.BaseController;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.domain.R;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.validate.AddGroup;
import com.kxmall.common.core.validate.EditGroup;
import com.kxmall.common.enums.BusinessType;
import com.kxmall.common.utils.poi.ExcelUtil;
import com.kxmall.vehicle.domain.bo.KxVehicleBoxGoodsBo;
import com.kxmall.vehicle.domain.bo.KxVehicleInGoodsBo;
import com.kxmall.vehicle.domain.bo.WarningVehicleBo;
import com.kxmall.vehicle.domain.vo.KxVehicleBoxGoodsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 餐车商品
 */
@Slf4j
@Api(tags = "餐车商品")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/vehicle/goods")
public class KxVehicleGoodsController extends BaseController {

    private final IKxVehicleGoodsService iKxVehicleGoodsService;
    /**
     * 查询全部餐车商品
     */
    @SaIgnore
    @ApiOperation(value = "/vehicle/goods/listAll - 查询全部餐车商品", httpMethod = "POST")
    @PostMapping("/listAll")
    public R<List<KxVehicleBoxGoodsVo>> listAll(KxVehicleBoxGoodsBo bo, HttpServletResponse response) {
        List<KxVehicleBoxGoodsVo> list = iKxVehicleGoodsService.queryList(bo);
        log.info(list.toString());
        return R.ok(list);
    }

    /**
     * 分页查询餐车商品列表
     */
    @SaCheckPermission("vehicle:goods:pageList")
    @ApiOperation(value = "/vehicle/goods/pageList - 分页查询餐车商品列表", httpMethod = "GET")
    @GetMapping("/pageList")
    public TableDataInfo<KxVehicleBoxGoodsVo> pageList(KxVehicleBoxGoodsBo bo, PageQuery pageQuery) {
        TableDataInfo< KxVehicleBoxGoodsVo> infoList = iKxVehicleGoodsService.queryPageList(bo, pageQuery);
        log.info(infoList.toString());
        return infoList;
    }

    /**
     * 导出餐车商品列表
     */
    @SaCheckPermission("vehicle:goods:export")
    @Log(title = "餐车商品", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "/vehicle/goods/export - 导出餐车商品列表", httpMethod = "POST")
    @PostMapping("/export")
    public void export(KxVehicleBoxGoodsBo bo, HttpServletResponse response) {
        List<KxVehicleBoxGoodsVo> list = iKxVehicleGoodsService.queryList(bo);
        log.info(list.toString());
        ExcelUtil.exportExcel(list, "餐车商品", KxVehicleBoxGoodsVo.class, response);
    }

    /**
     * 获取餐车商品详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("vehicle:goods:query")
    @ApiOperation(value = "/vehicle/goods/query/{id} - 获取餐车商品详细信息", httpMethod = "GET")
    @GetMapping("/query/{id}")
    public R<KxVehicleBoxGoodsVo> query(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        KxVehicleBoxGoodsVo kxVehicleBoxGoodsVo = iKxVehicleGoodsService.queryById(id);
        log.info(kxVehicleBoxGoodsVo.toString());
        return R.ok(kxVehicleBoxGoodsVo);
    }

    /**
     * 新增餐车商品
     */
    @SaCheckPermission("vehicle:goods:add")
    @Log(title = "新增餐车商品", businessType = BusinessType.INSERT)
    @ApiOperation(value = "/vehicle/goods/add - 新增餐车商品", httpMethod = "POST")
    @PostMapping("/add")
    @RepeatSubmit()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody KxVehicleInGoodsBo bo) {
        return toAjax(iKxVehicleGoodsService.insertByBo(bo));
    }

    /**
     * 修改餐车商品
     */
    @SaCheckPermission("vehicle:goods:edit")
    @Log(title = "修改餐车商品", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "/vehicle/goods/edit - 修改餐车商品", httpMethod = "PUT")
    @PutMapping("/edit")
    @RepeatSubmit()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody KxVehicleInGoodsBo bo) {
        return toAjax(iKxVehicleGoodsService.updateByBo(bo));
    }

    /**
     * 删除餐车商品
     *
     * @param ids 主键串
     */
    @SaCheckPermission("vehicle:goods:remove")
    @Log(title = "删除餐车商品", businessType = BusinessType.DELETE)
    @ApiOperation(value = "/vehicle/goods/remove/{ids} - 删除餐车商品", httpMethod = "DELETE")
    @DeleteMapping("/remove/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iKxVehicleGoodsService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 上下架
     */
    @ApiOperation(value = "/vehicle/goods/updateVehicleGoodsStatus - 上下架", httpMethod = "POST")
    @PostMapping("/updateVehicleGoodsStatus")
    public R<Boolean> updateVehicleGoodsStatus(@RequestBody KxVehicleInGoodsBo bo){
        return R.ok(iKxVehicleGoodsService.freezeOrActivation(bo));
    }

    /**
     * 逻辑删除
     */
    @ApiOperation(value = "/vehicle/goods/updateByVehicle - 逻辑删除", httpMethod = "POST")
    @PostMapping("/updateByVehicle")
    public R<Boolean> updateByVehicle(@RequestBody KxVehicleInGoodsBo bo){
        return R.ok(iKxVehicleGoodsService.updateByVehicle(bo));
    }

    /**
     * 更新当前价格
     */
    @ApiOperation(value = "/vehicle/goods/updateVehicleGoodsPrice - 更新当前价格", httpMethod = "POST")
    @PostMapping("/updateVehicleGoodsPrice")
    public R<Boolean> updateVehicleGoodsPrice(@RequestBody KxVehicleInGoodsBo bo){
        return R.ok(iKxVehicleGoodsService.updatePrice(bo));
    }

    /**
     * 库存预警列表
     */
    @ApiOperation(value = "/vehicle/goods/warningList - 库存预警列表", httpMethod = "GET")
    @GetMapping("/warningList")
    public TableDataInfo<KxVehicleBoxGoodsVo> warningList(WarningVehicleBo bo, PageQuery pageQuery) {
        return iKxVehicleGoodsService.queryPageWarningList(bo, pageQuery);
    }

    /**
     * 设置预警量
     */
    @ApiOperation(value = "/vehicle/goods/warningUpdate - 设置预警量", httpMethod = "POST")
    @PostMapping("/warningUpdate")
    public R<Boolean> warningUpdate(@RequestBody WarningVehicleBo bo) {
        return R.ok(iKxVehicleGoodsService.warningUpdate(bo));
    }


}
